<?php
require_once( '../system/smarty.inc' );
require_once( '../system/mdao/Class_D_DNPW_AREA.php' );
require_once( '../system/mdao/Class_M_PATTERN.php' );

require_once( '../system/mdao/Class_D_DNPW_PATTERN.php');
require_once( '../system/mdao/Class_M_OTHER.php' );
require_once( '../system/Class_ERROR.php' );
require_once( '../system/Class_PWD.php' );
require_once( '../system/login.inc.php' );

Class_PWD::valid_auth(AOKI_AUTH_DNP_AREA, $login['staff_auth']);

$editflg = 1; //1 編集可能　2 部数だけ編集可能
$is_shop = 2; //1 店舗可能　2　店舗不可 
$isG5 = FALSE ;
if   ($login['staff_auth'] == 'G-5') {
	if( isset($_GET['isshop']) && $_GET['isshop'] != '' ){
		echo "アクセス不可";
		exit;
	}
	$isG5 = TRUE ;
	$editflg = 2;
}

$dao  = new Class_D_DNPW_AREA ;
$daoP = new Class_D_DNPW_PATTERN ;

if( $_SERVER['REQUEST_METHOD'] == 'GET' ){
	$phase = 'input';
	if ( isset($_GET['w']) && $_GET['w'] != '' ){
		$dao->setTableNameWeek($_GET['w']);
		$matrix_cd = $_GET['w'] ;
		
		$titledata = Class_M_OTHER::get_titles( $matrix_cd, $_GET['p']  );
		$wData     = Class_M_OTHER::get_by_matrix_cd( $matrix_cd );
		
		$dispweek = $wData["year4"].'年'.$wData["week_cd"].'週';
		if ( $wData["[week_seq"] ) {
			$dispweek = $dispweek."回";
		}
		$dispweek = $dispweek."(". Class_PWD::int8_to_date($wData["flier_date"]) .")";
		
	}else {
		echo "パターンエラー";
		exit;
	}
	
	$forms  =  $dao->seaarchPAT( $_GET['w'], $_GET['p'] );
	$data  =  $dao->getPATTitles( $_GET['w'], $_GET['p'] );
	$sum_shops = 0;
	$sum_nums  = 0;
	$sum_ins   = 0;
	foreach ($data as $k=>$v){
		$sum_shops = $sum_shops + $v["shop_cnt"];
		$sum_nums  = $sum_nums  + $v["shop_nums"];
		$sum_ins   = $sum_ins   + $v["ins_nums"];
		
		$data[$k]["title_name"] = $titledata[$v["title_cd"]]["titlecell"];
		$data[$k]["size"] = $titledata[$v["title_cd"]]["size"];
		$titelSort   = $titledata[$v["title_cd"]]["map"];
		$data[$k]["isirgyu"] = $aoki_map_sort[$titelSort];
	}
	//$forms["area_nums"] = $sum_nums;
	$forms["sum_nums"]  = $sum_nums;
	$forms["sum_shops"] = $sum_shops;
	$forms["sum_ins"]   = $sum_ins;
	
	//print_r($data);
	//$dists  =  Class_M_OTHER::get_dist_array( );
	//$hands  =  Class_M_OTHER::get_hand_array( );
	//print_r($hands);
	//$forms["dist_name"] = $dists[$forms["dist_id"]];
	//$forms["hand_name"] = $hands[ $forms["hand_cd"] ];
	
	//$daoadshop = new Class_D_DNPW_AREA_SHOP($dao->get_db());
	//$daoadshop ->setTableNameWeek($_GET['w']);
	/*
	$titelSort = NULL;
	if ($forms["title_cd"]) {
		$titelSort = $titledata[$forms["title_cd"]]["map"];
	}
	if  ($titelSort){
		$dbarray = $dao->getShops($_GET['id'], $forms["pattern_cd"],$titelSort );
	}else {
		$dbarray = $dao->getShops($_GET['id'], $forms["pattern_cd"]);
	}
	
	//データ整形
	foreach ($dbarray as $k=>$v) {
		$forms["mutirow"] []=array("shop_cd"=> $v["shop_cd"], 
			"shop_name" => $v["shop_name"], 
			"map_no"    => $v["map_no"],
			"year_sale" => $v["year_sale"], 
			"shop_per"  => $v["shop_per"],
			"shop_nums" => $v["shop_nums"]);
	}
	
	$formsc_shop_per  =  0;
	$formsc_shop_nums =  0;
	if ( $forms["center_flg"] == "1" ) {
		foreach ($dbarray as $k=>$v) {
			$formsc_shop_per = $formsc_shop_per   + $v["shop_per"];
			$formsc_shop_nums = $formsc_shop_nums + $v["shop_nums"];
		}
	}

	$pdao = new Class_M_PATTERN ;  
	if ( isset($_GET['p']) && $_GET['p'] != '' ){
		if ( $patterns  = $pdao->get( $_GET['p'] ) ) {
			$forms["pattern_cd"]   =  $patterns["pattern_cd"];
			$forms["pattern_name"] =  $patterns["pattern_name"];
		}
	}else {
		echo "パターンエラー";
		exit;
	}
	
	
	if( isset( $forms['center_flg'] ) && ($forms['center_flg'] == '1') ){
		 $forms['hcenter_flg'] = 1;
	}else {
		 $forms['hcenter_flg'] = 0;
	}
	//都心計算
	$centernums =  $dao->getcentercount( $forms["pattern_cd"] );

	$centernums["c_area_nums"] = empty($centernums["c_area_nums"]) ? 0 : $centernums["c_area_nums"] ;
	$centernums["c_shop_per"]  = empty($centernums["c_shop_per"] ) ? 0 : $centernums["c_shop_per"] ;
	$centernums["c_shop_nums"] = empty($centernums["c_shop_nums"]) ? 0 : $centernums["c_shop_nums"] ;
	//$centernums["c_area_nums_txt"]  = $centernums["c_area_nums"];
	
	if( isset($_GET['id']) && $_GET['id'] != '' ){
		if ( $forms["center_flg"] == "1" ) {
			$forms["c_area_nums"] = $centernums["c_area_nums"] -  $forms["area_nums"] ;
			$forms["c_shop_per"]  = $centernums["c_shop_per"]  -  $formsc_shop_per ;
			$forms["c_shop_nums"] = $centernums["c_shop_nums"] -  $formsc_shop_nums ;
		}
	}
	*/
}else{
	$forms   =   $_POST;
	
	//$dao->setTableNameWeek($forms['w']);
	//echo "**".$is_shop."**";
	/*
	if (isset($forms["shop_cd"])) {
		foreach ($forms["shop_cd"] as $k=>$v) {
			$forms["mutirow"] []=array("shop_cd"=> $v, 
				"shop_name"  => $forms["shop_name"][$k],
				"year_sale"  => $forms["year_sale"][$k], 
				"map_no"     => $forms["map_no"][$k],
				"shop_per"   => $forms["shop_per"][$k],
				"shop_nums"  => $forms["shop_nums"][$k]);
		}
	} 
	*/
	//$titledata =  Class_M_OTHER::get_titles($forms['w'], $forms["pattern_cd"] );

	if( $forms['mode'] == 'input' ){
		
		$err     = new Class_ERROR;
		
		/*
		$chk     = $daoP->get_checks();
		
		foreach ($chk as $kcol => $chkval) {
			$err_mes[$kcol]  = $err->check( $forms[$kcol], $chkval );
		}
		*/
		
		/*
		$index = 0;
		$daoadshop =new Class_D_DNPW_AREA_SHOP($dao->get_db());
		$daoadshop ->setTableNameWeek($forms['w']);
		$chkadshop     = $daoadshop->get_checks();
		foreach($forms["mutirow"] as $k=>$v) {
			$err_mes["shop_per"][$index]   = $err->check( $v["shop_per"],  $chkadshop["shop_per"] );
			$err_mes["shop_nums"][$index]  = $err->check( $v["shop_nums"], $chkadshop["shop_nums"] );
			$index = $index + 1;
		}
		$forms["dist_id"]           =  Class_M_OTHER::get_dist_id($forms["dist_name"]);
		if( $forms['id'] != '' ){
			$err_mes["mutirow"] = $err->check( $forms["mutirow"],array( "EXIST" ));
			$where = sprintf(" pattern_cd = '%s' and dist_id  = '%s'  and id <> '%s'", $forms['pattern_cd'], $forms["dist_id"], $forms['id'] );
			if ( $dao->exits($where) ) {
					$err->clear = false;
					$err_mes['dist_id'] = '<p class="error">このパターンエリアは重複しています</p>';
				}
		}else {
			if( $err_mes['dist_id'] == '' ){
				$where = sprintf(" pattern_cd = '%s' AND dist_id ='%s' ", $forms['pattern_cd'], $forms["dist_id"] );
				if ( $dao->exits($where) ) {
					$err->clear = false;
					$err_mes['dist_id'] = '<p class="error">このパターンエリアは重複しています</p>';
				}
			}
		}
		*/
		
		if( $err->clear ){
			if( $forms['id'] != '' ){
				try {
					$dao->begin_trans();
					$executeflg = TRUE;
					$values = array();
					$values["memo"]   =  $forms["memo"] ;
					$values["area_nums"]   =  is_numeric( $forms["area_nums"] ) ?  $forms["area_nums"]  :0 ;
					if  (!($daoP->edit($forms["id"], $values))){
						$executeflg = FALSE;
					}
					if ($executeflg) {
						$dao->commit_trans();
						$smarty->assign('message', '登録が完了しました。');
					} else {
						$dao->rollback_trans();
						$smarty->assign('message', '登録が失敗しました。');
					}
				} catch (Exception $e) {
						$dao->rollback_trans();
						$smarty->assign('message', 'ＤＢエラーで失敗しました。');
				}
			}
			$phase = 'complete';
		}else{
			$phase = 'input';
		}
	}
}

	$forms["w"]      = empty( $forms["w"]  )? $matrix_cd: $forms["w"] ;
	$forms["dispw"]      = empty( $forms["dispw"]  )? $dispweek : $forms["dispw"] ;
	
	
	if( isset( $forms ) ) $smarty->assign('forms', $forms);

	
	if( isset( $data ) ) $smarty->assign('data', $data);
	if( isset( $err_mes ) ) $smarty->assign('err', $err_mes);
	
	
	$smarty->assign('titledata', $titledata );
	$titles = array();
	$titles[0] = PLEASE_SELECT_ARR;
	foreach ($titledata as $k => $v) {
		$titles[$k] = $v["titlecell"];
	}
	$smarty->assign('titles', $titles );
		
	$smarty->assign('phase', $phase );
	$smarty->assign('menu', $smarty->fetch('menu.html') );
	$smarty->assign('logout', $smarty->fetch('logout.html') );
	$smarty->assign('footer', $smarty->fetch('footer.html') );
	
	$smarty->display('ddnpwarea_form_pat1.html');
	
	
	
?>